﻿using System;

using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Web.DL.Project;
using PowerCreator.LMS.DL.Project.Entity;
using PowerCreator.LMS.ViewQuery;

public partial class sysadmin_DL_Project_AddRule : PowerCreator.LMS.Web.Base.WebBasePage
{
    TORuleClass objRC = new TORuleClass();
    TORule objRule = new TORule();
    TOFormulaOfRule objFR = new TOFormulaOfRule();
    VOTORule vo = new VOTORule();
    VOTOFormulaOfRule voFR = new VOTOFormulaOfRule();
    PowerCreator.LMS.Web.Course.Course opcourse = new PowerCreator.LMS.Web.Course.Course();
    PowerCreator.LMS.Course.Entity.VOTOCourse voCourse = new PowerCreator.LMS.Course.Entity.VOTOCourse();
    TOBatch objBatch = new TOBatch();
    private PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();
    private string url = "../../login.aspx";
    ViewQuery objView = new ViewQuery();

    protected void Page_Load(object sender, EventArgs e)
    {
        //if (IsLicensed(PowerCreator.LMS.Action.Core.ActionInfo.AddJob))
        //{
        //    Script.RedirectURL(GetRefUrl());
        //}
        CheckUser();
        if (!IsPostBack)
        {
            InitPage();
            int ARID = 0;
            //BinderClass();
            ViewState["CourseID"] = "0";
            if (Request.QueryString["CourseID"] != null)
            {
                string courseid = Request.QueryString["CourseID"].ToString();
                ViewState["CourseID"] = courseid;

                BinderBatch();
                voCourse = opcourse.LoadVOTCourse(int.Parse(courseid));
                lbl_Course.Text = voCourse.CourseName + "&nbsp;&nbsp;";
            }

            if (Request.QueryString["ARID"] != null)
            {
                ARID = int.Parse(Request.QueryString["ARID"].ToString());
                ViewState["ARID"] = ARID.ToString();
                BinderForm(ARID);
            }
            //ViewState["FormulaOfRuleID"] = 0;

        }
    }

    private void CheckUser()
    {
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            Script.RedirectNewUrl(url);
        }
        else
        {
            login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
            if (login == null)
            {
                Script.RedirectNewUrl(url);
            }
            else
            {
                if (login.VOAdvancedUser == null)
                {
                    Script.RedirectNewUrl(url);
                }
                else if (login.VOAdvancedUser.AUserGroup > 2)
                {
                    //Response.Write(login.VOAdvancedUser.AUserGroup.ToString());
                    //Response.End();
                    Script.ShowAndHistory("您无此权限！", -1);
                }
            }
        }


    }
    protected override void InitPage()
    {
        //lbl_InfoTitle.Text = GetLanageValueByKey("lbl_AddJob_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_JobName.Text = GetLanageValueByKey("lbl_AddJob_JobName", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_JobDescription.Text = GetLanageValueByKey("lbl_AddJob_JobDescription", PowerCreator.LMS.Language.LanagetItemType.Page);

        //btnSave.Text = GetLanageValueByKey("btn_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
        //btnReset.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);
        //base.InitPage();

        //lbl_InfoTitle_AddNewType.Text = GetLanageValueByKey("lab_AddNewType_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_NewTypeName.Text = GetLanageValueByKey("lab_AddNewType_TypeName", PowerCreator.LMS.Language.LanagetItemType.Page);

        btnSave.Text = GetLanageValueByKey("btn_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnReset.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);
        base.InitPage();
    }
    //private void BinderClass()
    //{
    //    DataTable dt = new DataTable();

    //    dt = objRC.Query("");
    //    ddl_RuleClass.DataSource = dt;
    //    ddl_RuleClass.DataTextField = "RuleClass";
    //    ddl_RuleClass.DataValueField = "ID";
    //    ddl_RuleClass.DataBind();

    //}
    private void BinderBatch()
    {
        DataTable dt = new DataTable();

        dt = objBatch.Query("");
        ddl_Batch.DataSource = dt;
        ddl_Batch.DataTextField = "Batch";
        ddl_Batch.DataValueField = "Batch_Id";
        ddl_Batch.DataBind();

    }
    private void BinderForm(int id)
    {
        vo = objRule.Load(id);
        txt_AssessmentRule.Text = vo.RuleName;
        //ddl_RuleClass.SelectedValue = vo.RuleClassID.ToString();
        ddl_Batch.SelectedValue = vo.FKBatchID.ToString();

        DataTable dt = new DataTable();

        dt = objFR.Query("RuleID="+id.ToString());
        if (dt.Rows.Count == 1)
        {
            txt_TotalTime_Low.Text = dt.Rows[0]["TotalTime_Low"].ToString();
            txt_TotalTime_High.Text = dt.Rows[0]["TotalTime_High"].ToString();
            txt_TotalTimeWeightA.Text = dt.Rows[0]["TotalTimeWeightA"].ToString();
            txt_WorkFinishTimes_Low.Text = dt.Rows[0]["WorkFinishTimes_Low"].ToString();
            txt_WorkFinishTimes_High.Text = dt.Rows[0]["WorkFinishTimes_High"].ToString();
            txt_WorkFinishTimesWeightB.Text = dt.Rows[0]["WorkFinishTimesWeightB"].ToString();
            txt_WorkCommentTimes_Low.Text = dt.Rows[0]["WorkCommentTimes_Low"].ToString();
            txt_WorkCommentTimes_High.Text = dt.Rows[0]["WorkCommentTimes_High"].ToString();
            txt_WorkCommentTimesWeightB.Text = dt.Rows[0]["WorkCommentTimesWeightB"].ToString();
            txt_WorkTatolWeigthA.Text = dt.Rows[0]["WorkTatolWeigthA"].ToString();
            txt_Score_Low.Text = dt.Rows[0]["Score_Low"].ToString();
            txt_Score_High.Text = dt.Rows[0]["Score_High"].ToString();
            txt_ScoreWeightA.Text = dt.Rows[0]["ScoreWeightA"].ToString();
            //ViewState["FormulaOfRuleID"] = dt.Rows[0]["ID"].ToString();
        }
        
        //BinderProvince(ViewState["ARID"].ToString());
    }
    
    private bool CheckPageData()
    {
        try
        {
            int iScore_low = int.Parse(txt_Score_Low.Text.Trim());
            int iScore_High = int.Parse(txt_Score_High.Text.Trim());
            int iScoreWeightA = int.Parse(txt_ScoreWeightA.Text.Trim());
            double iTotalTime_Low = double.Parse(txt_TotalTime_Low.Text.Trim());
            double iTotalTime_Hight = double.Parse(txt_TotalTime_High.Text.Trim());
            int iTotalTimeWeightA = int.Parse(txt_TotalTimeWeightA.Text.Trim());
            int iWorkCommentTimes_Low = int.Parse(txt_WorkCommentTimes_Low.Text.Trim());
            int iWorkCommentTimes_High = int.Parse(txt_WorkCommentTimes_High.Text.Trim());
            int iWorkCommentTimesWeightB = int.Parse(txt_WorkCommentTimesWeightB.Text.Trim());
            int iWorkFinishTimes_Low = int.Parse(txt_WorkFinishTimes_Low.Text.Trim());
            int iWorkFinishTimes_High = int.Parse(txt_WorkFinishTimes_High.Text.Trim());
            int iWorkFinishTimesWeightB = int.Parse(txt_WorkFinishTimesWeightB.Text.Trim());
            int iWorkTatolWeigthA = int.Parse(txt_WorkTatolWeigthA.Text.Trim());
        }
        catch
        {
            lbl_msg_Error.Text = "<br />考核规则公式中的每一项都必填，学习时间可以为小数，其余每一项都必须是正整数，请检查！";
            return false;
        }
        string search = string.Format("RuleName = '{0}'",txt_AssessmentRule.Text.Trim());
        if (ViewState["ARID"] != null)
        {
            search += " and Rule_Id<>" + ViewState["ARID"].ToString();
        }

        if (objRule.Count(search) != 0)
        {

            //lbl_msg_Error.Text = GetLanageValueByKey("lbl_msg_addjob_aspx_count", PowerCreator.LMS.Language.LanagetItemType.MSG);

            lbl_msg_Error.Text = "<br />" + GetLanageValueByKey("msg_AddNewType_checkMsg", PowerCreator.LMS.Language.LanagetItemType.Page);
            return false;
        }
        if ((int.Parse(txt_TotalTimeWeightA.Text.Trim()) + int.Parse(txt_ScoreWeightA.Text.Trim()) + int.Parse(txt_WorkTatolWeigthA.Text.Trim())) != 100)
        {
            lbl_msg_Error.Text = "<br />权重a三项加起来的总和必须为100";
            return false;
        }
        if ((int.Parse(txt_WorkFinishTimesWeightB.Text.Trim()) + int.Parse(txt_WorkCommentTimesWeightB.Text.Trim())) != 100)
        {
            lbl_msg_Error.Text = "<br />权重b两项加起来的总和必须为100";
            return false;
        }
        return true;
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (!CheckPageData()) return;
        DataTable dtView = new DataTable();
        string strSeach = "select * from to_rule where FKBatchID={0} and FKCourseID=" + ViewState["CourseID"];
        strSeach = string.Format(strSeach, int.Parse(ddl_Batch.SelectedValue));
        dtView = objView.Query(strSeach);
        
        if (ViewState["ARID"] == null)
        {
            if (dtView.Rows.Count > 0)
            {
                Script.Alert("该批次的考核规则已经添加进去，不能再添加！");
                return;
            }
            vo.RuleName = txt_AssessmentRule.Text.Trim();
            vo.FKBatchID = int.Parse(ddl_Batch.SelectedValue);
            vo.FKCourseID=int.Parse(ViewState["CourseID"] .ToString());
            vo.RuleDate = DateTime.Now;
            int RuleID = 0;
            if (objRule.Add(vo,ref RuleID))
            {

                voFR.RuleID = RuleID;
                voFR.Score_Low = int.Parse(txt_Score_Low.Text.Trim());
                voFR.Score_High = int.Parse(txt_Score_High.Text.Trim());
                voFR.ScoreWeightA = int.Parse(txt_ScoreWeightA.Text.Trim());
                voFR.TotalTime_Low = double.Parse(txt_TotalTime_Low.Text.Trim());
                voFR.TotalTime_High = double.Parse(txt_TotalTime_High.Text.Trim());
                voFR.TotalTimeWeightA = int.Parse(txt_TotalTimeWeightA.Text.Trim());
                voFR.WorkCommentTimes_Low = int.Parse(txt_WorkCommentTimes_Low.Text.Trim());
                voFR.WorkCommentTimes_High = int.Parse(txt_WorkCommentTimes_High.Text.Trim());
                voFR.WorkCommentTimesWeightB = int.Parse(txt_WorkCommentTimesWeightB.Text.Trim());
                voFR.WorkFinishTimes_Low = int.Parse(txt_WorkFinishTimes_Low.Text.Trim());
                voFR.WorkFinishTimes_High = int.Parse(txt_WorkFinishTimes_High.Text.Trim());
                voFR.WorkFinishTimesWeightB = int.Parse(txt_WorkFinishTimesWeightB.Text.Trim());
                voFR.WorkTatolWeigthA = int.Parse(txt_WorkTatolWeigthA.Text.Trim());
                if (!objFR.Add(voFR))
                {
                    objRule.Del(RuleID);
                }
                string url = "ManageRule.aspx?CourseID=" + ViewState["CourseID"].ToString();
                Script.RedirectURL(url);
            }
        }
        else
        {
            vo = objRule.Load(int.Parse(ViewState["ARID"].ToString()));
            vo.RuleName = txt_AssessmentRule.Text.Trim();
            vo.FKBatchID = int.Parse(ddl_Batch.SelectedValue);
            vo.FKCourseID = int.Parse(ViewState["CourseID"].ToString());
            vo.RuleDate = DateTime.Now;
            if (objRule.Update(vo))
            {
                voFR.RuleID = vo.Rule_Id;
                voFR.Score_Low = int.Parse(txt_Score_Low.Text.Trim());
                voFR.Score_High = int.Parse(txt_Score_High.Text.Trim());
                voFR.ScoreWeightA = int.Parse(txt_ScoreWeightA.Text.Trim());
                voFR.TotalTime_Low = double.Parse(txt_TotalTime_Low.Text.Trim());
                voFR.TotalTime_High = double.Parse(txt_TotalTime_High.Text.Trim());
                voFR.TotalTimeWeightA = int.Parse(txt_TotalTimeWeightA.Text.Trim());
                voFR.WorkCommentTimes_Low = int.Parse(txt_WorkCommentTimes_Low.Text.Trim());
                voFR.WorkCommentTimes_High = int.Parse(txt_WorkCommentTimes_High.Text.Trim());
                voFR.WorkCommentTimesWeightB = int.Parse(txt_WorkCommentTimesWeightB.Text.Trim());
                voFR.WorkFinishTimes_Low = int.Parse(txt_WorkFinishTimes_Low.Text.Trim());
                voFR.WorkFinishTimes_High = int.Parse(txt_WorkFinishTimes_High.Text.Trim());
                voFR.WorkFinishTimesWeightB = int.Parse(txt_WorkFinishTimesWeightB.Text.Trim());
                voFR.WorkTatolWeigthA = int.Parse(txt_WorkTatolWeigthA.Text.Trim());


                DataTable dt = new DataTable();
                dt = objFR.Query("RuleID=" + vo.Rule_Id.ToString());
                if (dt.Rows.Count == 1)
                {
                    voFR.ID = int.Parse(dt.Rows[0]["ID"].ToString());
                    objFR.Update(voFR);
                }
                else
                {
                    objFR.Add(voFR);
                }


                string url = "ManageRule.aspx?CourseID=" + ViewState["CourseID"].ToString();
                Script.RedirectURL(url);
            }
        }
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        string url = "ManageRule.aspx?CourseID=" + ViewState["CourseID"].ToString();
        Script.RedirectURL(url);
    }
}